package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.*


import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 作者：SimGa
 * 日期：2020/12/3
 * 时间：17:04
 * 包名：com.vitabuybuy.tv.network
 */
object LiveRepository {

    fun getFirstCategory(): IAwait<List<FirstCategoryDto>> {
        return RxHttp.postJson(NetUrl.api_shop_first_category)
            .add("category", mapOf(Pair("type", "Store")))
            .toResponse()
    }

    fun getLiveList(
        params: HashMap<String, Any>,
        sort: String,
        pageNumber: Int
    ): IAwait<List<LiveListDto>> {
        params["pageable"] = mapOf(
            Pair("page", pageNumber),
            Pair("size", 10),
            Pair("desc", sort)
        )
        return RxHttp.postJson(NetUrl.api_home_live_list)
            .addAll(params).toResponse()

    }

    fun getCateSearchGoodList(
        content: Any,
        storeId: Int?,
        sort: String,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<List<GoodsDto>> {
        val map = if (storeId != null && storeId != 0) {
            mapOf(
                if (content is String) {
                    Pair("product", mapOf(Pair("name", content)))
                } else {
                    Pair("categoryList", arrayOf(mapOf(Pair("id", content))))
                },
                Pair("storeId", storeId),
                Pair(
                    "pageable",
                    mapOf(
                        Pair("page", pageNumber),
                        Pair("size", pageSize),
                        Pair(sort, "sale_price")
                    )
                )
            )
        } else {
            mapOf(
                if (content is String) {
                    Pair("product", mapOf(Pair("name", content)))
                } else {
                    Pair("categoryList", arrayOf(mapOf(Pair("id", content))))
                },
                Pair(
                    "pageable",
                    mapOf(
                        Pair("page", pageNumber),
                        Pair("size", pageSize),
                        Pair(sort, "sale_price")
                    )
                )
            )
        }
        return RxHttp.postJson(NetUrl.api_home_shop_goods)
            .addAll(map).toResponse()
    }

    fun getScreenGoodList(
        brandList2: MutableList<BrandDto>,
        salePriceDto: SalePriceDto,
        sort: String,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<List<GoodsDto>> {
        val map = mapOf(
            Pair(
                "pageable",
                mapOf(
                    Pair("page", pageNumber),
                    Pair("size", pageSize),
                    Pair(sort, "sale_price")
                )
            ),
            Pair("salePrice", salePriceDto),
            Pair("brandList", brandList2)
        )
        return RxHttp.postJson(NetUrl.api_home_shop_goods)
            .addAll(map).toResponse()
    }


    fun getDetails(goodId: Int): IAwait<GoodsDetailsDto> {
        return RxHttp.postJson(NetUrl.api_get_goods_details)
            .add("id", goodId).toResponse()
    }

    fun addToCart(goodId: Int, buyNum: Int): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_add_to_cart)
            .add("goods", mapOf(Pair("id", goodId)))
            .add("number", buyNum)
            .toResponse()
    }

    fun searchStoreList(content: String, pageNumber: Int): IAwait<List<StoreListDto>> {
        val json = mapOf(
            Pair(
                "pageable",
                mapOf(
                    Pair("page", pageNumber),
                    Pair("size", 10),
                    Pair("desc", "id")
                )
            ),
            Pair(
                "store", mapOf(
                    Pair("name", content)
                )
            )
        )
        return RxHttp.postJson(NetUrl.api_search_stroe).addAll(json).toResponse()
    }
}